﻿// =================================================================== 
// 项目说明
//====================================================================

// ===================================================================
using System;
using System.Data;
using CommonLib.Model;
using CommonLib.IDAL;
using CommonLib.DALFactory;
using System.Data.SqlClient;
namespace CommonLib.BLL
{
    public class BaseCommonBll
    {
        #region ----------变量定义----------
        ///<summary>
        ///
        ///</summary>
        public static readonly IBaseCommonDAL _dal = (IBaseCommonDAL)DataAccessFactory.CreateObject("BaseCommonDAL");
        #endregion


        #region ----------构造函数----------
        /// <summary>
        /// 构造函数

        /// </summary>
        public BaseCommonBll()
        {
        }
        #endregion

        public static string GetSequenceNumber2(string t_name)
        {
            return GetSequenceNumber(t_name, "orderno", 4);
        }

        public static string GetSequenceNumber(string t_name, string key_word, int len)
        {
            string sFmt=DateTime.Now.ToString("yyyyMM");
            int nId=CommLibStoreProcedure.PROC_GETNEWSNFORTABLE2(t_name, t_name, key_word, sFmt);
            string sId = nId.ToString().PadLeft(len,'0');
            return sFmt + sId;
        }


        /// <summary>
        /// 获取操作员对某个菜单的数据权限，兼容所有子部门
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="menuNo"></param>
        /// <returns></returns>
        public static string GetDeptIdList(string userid, string menuNo)
        {
            return _dal.GetDeptIdList(userid, menuNo);
        }

        public static string GetDeptIndexList(string userid, string menuNo)
        {
            return _dal.GetDeptIndexList(userid, menuNo);
        }

        /// <summary>
        /// 返回date1加上addtime后的时间
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="worktime">来源于sys_parameter中，指定格式为：{{"9:00","12:00"},{"13:00,"17:00"}}</param>
        /// <param name="addtime">间隔时间，n天n小时n分</param>
        /// <returns></returns>
        public static DateTime WorkTimeAdd(DateTime date1, TimeSpan addtime, string[,] worktime)
        {
            DateTime rtnDate = date1;
            DateTime tarEndTime = date1.AddDays(addtime.Days).AddHours(addtime.Hours).AddMinutes(addtime.Minutes).AddSeconds(addtime.Seconds);
            DateTime endTime = date1;
            TimeSpan tsVar = new TimeSpan();
            //比较以天为准
            DateTime eTDay = DateTime.Parse(endTime.ToShortDateString());
            DateTime teTDay = DateTime.Parse(tarEndTime.ToShortDateString());
            if (eTDay < teTDay)
            {
                while (eTDay < teTDay)
                {
                    rtnDate = rtnDate.AddDays(1);
                    tsVar = WorkTimeDiff(date1, rtnDate, worktime);
                    endTime = date1.AddDays(tsVar.Days).AddHours(tsVar.Hours).AddMinutes(tsVar.Minutes);
                    eTDay = DateTime.Parse(endTime.ToShortDateString());
                }
            }
            //比较以小时为准

            DateTime eTHour = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":00:00");
            DateTime teTHour = DateTime.Parse(tarEndTime.ToShortDateString() + " " + tarEndTime.Hour.ToString() + ":00:00");
            if (eTHour < teTHour)
            {
                while (eTHour < teTHour)
                {
                    rtnDate = rtnDate.AddHours(1);
                    tsVar = WorkTimeDiff(date1, rtnDate, worktime);
                    endTime = date1.AddDays(tsVar.Days).AddHours(tsVar.Hours).AddMinutes(tsVar.Minutes);
                    eTHour = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":00:00");
                }
            }
            //比较以分钟为准

            DateTime eTMinute = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":" + endTime.Minute.ToString() + ":00");
            DateTime teTMinute = DateTime.Parse(tarEndTime.ToShortDateString() + " " + tarEndTime.Hour.ToString() + ":" + tarEndTime.Minute.ToString() + ":00");
            if (eTMinute < teTMinute)
            {
                while (eTMinute < teTMinute)
                {
                    rtnDate = rtnDate.AddMinutes(1);
                    tsVar = WorkTimeDiff(date1, rtnDate, worktime);
                    endTime = date1.AddDays(tsVar.Days).AddHours(tsVar.Hours).AddMinutes(tsVar.Minutes);
                    eTMinute = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":" + endTime.Minute.ToString() + ":00");
                }
            }
            //比较以秒为准
            DateTime eTSecond = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":" + endTime.Minute.ToString() + ":" + endTime.Second.ToString());
            DateTime teTSecond = DateTime.Parse(tarEndTime.ToShortDateString() + " " + tarEndTime.Hour.ToString() + ":" + tarEndTime.Minute.ToString() + ":" + tarEndTime.Second.ToString());
            if (eTSecond < teTSecond)
            {
                while (eTSecond < teTSecond)
                {
                    rtnDate = rtnDate.AddSeconds(1);
                    tsVar = WorkTimeDiff(date1, rtnDate, worktime);
                    endTime = date1.AddDays(tsVar.Days).AddHours(tsVar.Hours).AddMinutes(tsVar.Minutes).AddSeconds(tsVar.Seconds);
                    eTSecond = DateTime.Parse(endTime.ToShortDateString() + " " + endTime.Hour.ToString() + ":" + endTime.Minute.ToString() + ":" + endTime.Second.ToString());
                }
            }
            return rtnDate;
        }

        /// <summary>
        /// 返回两个时间中差距
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <param name="worktime"></param>
        /// <returns></returns>
        public static TimeSpan WorkTimeDiff(DateTime date1, DateTime date2, string[,] worktime)
        {
            TimeSpan Span = new TimeSpan();
            TimeSpan ts = DateTime.Parse("1900-01-01") - DateTime.Parse("1900-01-01");
            if (date2 <= date1)
                return ts;

            int endDays = (date2 - date1).Days;
            string strWhere1 = "  to_char(holiday,'yyyy-mm-dd')='" + date1.ToShortDateString() + "'";
            string strWhere2 = "  to_char(holiday,'yyyy-mm-dd')='" + date2.ToShortDateString() + "'";
            string strWhere3 = "  to_date('" + date1.ToShortDateString() + "','yyyy-mm-dd') <= holiday and holiday <= to_date('" + date2.ToShortDateString() + "','yyyy-mm-dd') ";
            int noWorkDays = NoWorkDays(strWhere3);
            if (noWorkDays > 0)
            {
                endDays = endDays - noWorkDays;
                if (NoWorkDays(strWhere1) > 0)
                    date1 = DateTime.Parse(date1.ToShortDateString());
                if (NoWorkDays(strWhere2) > 0)
                    date2 = DateTime.Parse(date2.ToShortDateString() + " " + "23:59:59");
            }
            TimeSpan tsOutYMD = WorkTimeDiffOutYMD(date1, date2, worktime);
            if (tsOutYMD.Days == 1)
            {
                endDays = endDays + 1;
                Span = new TimeSpan(endDays, 0, 0, 0);
            }
            else
                Span = new TimeSpan(endDays, tsOutYMD.Hours, tsOutYMD.Minutes, tsOutYMD.Seconds);
            return Span;
        }
        
        public static int NoWorkDays(string strWhere)
        {
            int days = 0;
            DataTable dtt = SYS_NOWORKDAY.GetList(strWhere).Tables[0];
            days = dtt.Rows.Count;
            return days;
        }
         
          
         
        /// <summary>
        /// 返回两个时间中撇开年月日后的差距

        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <param name="worktime"></param>
        /// <returns></returns>
        public static TimeSpan WorkTimeDiffOutYMD(DateTime date1, DateTime date2, string[,] worktime)
        {
            TimeSpan Span = new TimeSpan();
            TimeSpan ts = DateTime.Parse("1900-01-01") - DateTime.Parse("1900-01-01");
            string wt1 = worktime[0, 0];
            string wt2 = worktime[0, 1];
            string wt3 = worktime[1, 0];
            string wt4 = worktime[1, 1];
            DateTime dt1 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + date1.ToLongTimeString());
            DateTime dt2 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + date2.ToLongTimeString());
            DateTime wdt1 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + wt1);
            DateTime wdt2 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + wt2);
            DateTime wdt3 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + wt3);
            DateTime wdt4 = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + wt4);
            if (dt2 <= dt1)
                return ts;

            #region 当date1<wt1时

            if (dt1 < wdt1)
            {
                if (dt2 < wdt1)
                    Span = ts;
                else
                {
                    if (wdt1 <= dt2 && dt2 < wdt2)
                        Span = dt2 - wdt1;
                    else
                    {
                        if (wdt2 <= dt2 && dt2 < wdt3)
                            Span = wdt2 - wdt1;
                        else
                        {
                            if (wdt3 <= dt2 && dt2 < wdt4)
                                Span = (dt2 - wdt1) - (wdt3 - wdt2);
                            else
                            {
                                if (wdt4 <= dt2)
                                    Span = (wdt4 - wdt1) - (wdt3 - wdt2);
                            }
                        }
                    }
                }
            }
            #endregion

            #region 当wt1<date1<wt2时

            if (wdt1 <= dt1 && dt1 < wdt2)
            {
                if (wdt1 <= dt2 && dt2 < wdt2)
                    Span = dt2 - dt1;
                else
                {
                    if (wdt2 <= dt2 && dt2 < wdt3)
                        Span = wdt2 - dt1;
                    else
                    {
                        if (wdt3 <= dt2 && dt2 < wdt4)
                            Span = (dt2 - dt1) - (wdt3 - wdt2);
                        else
                        {
                            if (wdt4 <= dt2)
                                Span = (wdt4 - dt1) - (wdt3 - wdt2);
                        }
                    }
                }
            }
            #endregion

            #region 当wt2<date1<wt3时

            if (wdt2 < dt1 && dt1 < wdt3)
            {
                if (wdt2 <= dt2 && dt2 < wdt3)
                    Span = ts;
                else
                {
                    if (wdt3 <= dt2 && dt2 < wdt4)
                        Span = dt2 - wdt3;
                    else
                    {
                        if (wdt4 <= dt2)
                            Span = wdt4 - wdt3;
                    }
                }
            }
            #endregion

            #region 当wt3<date1<wt4时

            if (wdt3 < dt1 && dt1 < wdt4)
            {
                if (wdt3 <= dt2 && dt2 < wdt4)
                    Span = dt2 - dt1;
                else
                {
                    if (wdt4 <= dt2)
                        Span = wdt4 - dt1;
                }
            }
            #endregion

            #region 当wt4<date1时

            if (wdt4 < dt1)
            {
                Span = ts;
            }
            #endregion

            TimeSpan realwts = (wdt4 - wdt1).Subtract((wdt3 - wdt2));
            double wtHours = realwts.TotalHours;
            double tsHours = Span.TotalHours;
            if (tsHours >= wtHours)
                Span = new TimeSpan(1, 0, 0, 0, 0);
            return Span;
        }

        public static string conVert_Date(string sValue,string sFmt){
            return _dal.conVert_Date(sValue, sFmt);
        }

        public static string conVert_Date(string sValue)
        {
            return conVert_Date(sValue, "yyyy-mm-dd hh:mm");
        }

        public static string Field_Date(string fieldName, string Fmt)
        {
            return _dal.Field_Date(fieldName, Fmt);
        }

        public static int ExecSql(SqlTransaction transaction, string commandText)
        {
            return _dal.ExecSql(transaction, commandText);
        }

        public static DataSet ExecDataSetSql(SqlTransaction transaction, string commandText)
        {
            return _dal.ExecDataSetSql(transaction, commandText);
        }
    }
}
